package com.efeichong.project.service.impl;

import com.efeichong.common.exception.CustomException;
import com.efeichong.common.utils.OptionEmptyFunction;
import com.efeichong.jpa.JExample;
import com.efeichong.jpa.UpdateGlobalConfig;
import com.efeichong.project.domain.dao.db.SysDeptDao;
import com.efeichong.project.domain.dao.db.SysUserDao;
import com.efeichong.project.domain.entity.db.SysDept;
import com.efeichong.project.domain.entity.db.SysUser;
import com.efeichong.project.domain.vo.DeptUserVo;
import com.efeichong.project.domain.vo.SysDeptVo;
import com.efeichong.project.service.SysDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @date 2020/12/03
 * @description 部门表
 */
@RequiredArgsConstructor
@Service
public class SysDeptServiceImpl implements SysDeptService {

    private final SysDeptDao sysDeptDao;
    private final SysUserDao sysUserDao;

    /**
     * 通过id查询
     *
     * @param id 主键
     * @return
     */
    @Override
    public SysDeptVo getInfo(Long id) {
        return sysDeptDao.selectById(id, SysDeptVo.class).orElseThrow(OptionEmptyFunction.create());
    }

    /**
     * 分页查询
     *
     * @param sysDeptVo
     * @return
     */
    @Override
    public List<SysDeptVo> list(SysDeptVo sysDeptVo) {
        JExample<SysDept> example = new JExample();
        example.initExample(sysDeptVo);
        example.orderBy().asc("sort");
        return sysDeptDao.selectAll(example, SysDeptVo.class);
    }

    @Override
    public List<DeptUserVo> queryDeptUser(Long deptId) {
        JExample<SysDept> example = new JExample();
        example.orderBy().asc("sort");
        if (deptId == null) {
            example.createCriteria().andIsNull("parent");
        } else {
            example.createCriteria().andEqualTo("parent.id", deptId);
        }
        List<DeptUserVo> list = sysDeptDao.selectAll(example, DeptUserVo.class);
        if (deptId != null) {
            JExample<SysUser> userExample = new JExample<>();
            userExample.createCriteria().andEqualTo("sysDepts.id", deptId);
            List<DeptUserVo> users = sysUserDao.selectAll(userExample, DeptUserVo.class);
            for (DeptUserVo user : users) {
                user.setType(1);
                list.add(user);
            }
        }
        return list;
    }

    /**
     * 新增
     *
     * @param sysDeptVo
     * @return
     */
    @Transactional
    @Override
    public void insertSysDept(SysDeptVo sysDeptVo) {
        SysDept sysDept = sysDeptVo.toPo(SysDept.class);
        if (sysDeptVo.getParentId() != null) {
            SysDept parent = sysDeptDao.selectById(sysDeptVo.getParentId()).get();
            sysDept.setParent(parent);
            List<SysDept> parents = new ArrayList<>();
            for (SysDept orgParent : parent.getDeptParents()) {
                parents.add(orgParent);
            }
            SysDept parentShip = new SysDept();
            parentShip.setId(parent.getId());
            parents.add(parentShip);
            sysDept.setDeptParents(parents);
        }
        sysDeptDao.save(sysDept);
    }


    /**
     * 修改
     *
     * @param sysDeptVo
     * @return
     */
    @Transactional
    @Override
    public void updateSysDept(SysDeptVo sysDeptVo) {
        if (sysDeptVo.getId() == null) {
            throw new CustomException("主键为必传参数");
        }
        SysDept sysDept = sysDeptVo.toPo(SysDept.class);
        if (sysDeptVo.getParentId() != null) {
            SysDept parent = sysDeptDao.selectById(sysDeptVo.getParentId()).get();
            sysDept.setParent(parent);
            List<SysDept> parents = new ArrayList<>();
            while (parent != null) {
                SysDept parentShip = new SysDept();
                parentShip.setId(parent.getId());
                parents.add(parentShip);
                parent = parent.getParent();
            }
            sysDept.setDeptParents(parents);
        }
        sysDeptDao.save(sysDept, new UpdateGlobalConfig() {
            @Override
            public String[] ignoreUpdateProps() {
                return new String[0];
            }

            @Override
            public String[] canBeNullProps() {
                return new String[]{"leaders"};
            }
        });
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        sysDeptDao.deleteAllByIds(ids);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public void deleteById(Long id) {
        sysDeptDao.deleteById(id);
    }
}
